package com.naizhuai.Model.db.managenote;

import android.support.annotation.NonNull;

import com.naizhuai.Model.db.AppDatabase;
import com.naizhuai.Model.db.eneity.ManageNoteBean;
import com.naizhuai.base.AppExecutors;

import java.util.List;

/**
 * @author：code_han
 * @date 2018/5/29
 */
public class ManageNoteData implements ManageNoteSourse {
    private static volatile ManageNoteData INSTANCE;

    private @NonNull
    ManageNoteDao mManageNoteDao;

    private @NonNull
    AppExecutors mAppExecutors;

    public static ManageNoteData getInstance() {
        if (INSTANCE == null) {
            synchronized (ManageNoteData.class) {
                if (INSTANCE == null) {
                    INSTANCE = new ManageNoteData();
                }
            }
        }
        return INSTANCE;
    }

    public ManageNoteData() {
        mAppExecutors = new AppExecutors();
        mManageNoteDao = AppDatabase.getInstance().ManageNoteDao();
    }

    @Override
    public void getNoteDataAsyn(int status, GetManageNoteCallback callback) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                final List<ManageNoteBean> user = mManageNoteDao.searchUserByStatus(status);
                mAppExecutors.mainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        if (user != null) {
                            callback.onGetStateData(user);
                        } else {
                            callback.onDataNotAvailable();
                        }
                    }
                });
            }
        };
        mAppExecutors.diskIO().execute(runnable);
    }

    @Override
    public void saveNoteData(List<ManageNoteBean> search) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mManageNoteDao.saveManageNoteData(search);
            }
        };

        mAppExecutors.diskIO().execute(runnable);
    }

    @Override
    public void saveManageNoteSingleData(ManageNoteBean search) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mManageNoteDao.saveManageNoteSingleData(search);
            }
        };

        mAppExecutors.diskIO().execute(runnable);
    }


    @Override
    public void deleteManageNoteData(ManageNoteBean bean) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mManageNoteDao.deleteManageNoteData(bean);
            }
        };

        mAppExecutors.diskIO().execute(runnable);
    }


}
